package cn.com.huzo.service.oa.impl;

import cn.com.huzo.model.entities.oa.BulletinEntity;
import cn.com.huzo.repository.oa.BulletinRepository;
import cn.com.huzo.service.oa.BulletinManager;
import huzofw.model.entities.paginator.Paginator;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公告板管理服务对象类实现。
 *
 * @author 范学春
 */
@Service("BulletinManager")
public class BulletinManagerImpl extends GenericManagerImpl<BulletinEntity, String>
        implements BulletinManager {

    @Resource(name = "BulletinRepository")
    private BulletinRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public BulletinEntity fetchByKey(String key) throws Exception {
        return _dao.fetchByKey(key);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<BulletinEntity> fetchForDashboard() throws Exception {
        return _dao.fetchForDashboard();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<BulletinEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<BulletinEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public String insert(BulletinEntity instance) throws Exception {
        String key = _dao.insert(instance);
        // 审计日志
        if (key != null) {
            writeAuditLog(instance.getUpdatedBy(), "OA管理→公告板", "id：" + key, "增加");
        }
        return key;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(BulletinEntity instance) throws Exception {
        Integer rowsAffected = _dao.update(instance);
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(instance.getUpdatedBy(), "OA管理→公告板", "id：" + instance.getBulletinId(), "更新");
        }
        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer delete(BulletinEntity instance) throws Exception {
        // 删除
        Integer rowsAffected = _dao.delete(instance.getBulletinId());
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(instance.getDeletedBy(),
                    "OA管理→公告板", "id：" + instance.getBulletinId(),
                    "删除");
        }

        return rowsAffected;
    }
}
